unit uCadParcela;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, CurrEdit, DBCtrls, StdCtrls, ToolEdit, RXDBCtrl, Mask, ComCtrls,
  DB;

type
  TfCadParcela = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    Label1: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Edit1: TEdit;
    dbValorParcela: TRxDBCalcEdit;
    dbVencimento: TDBDateEdit;
    Button1: TButton;
    Button3: TButton;
    TabSheet2: TTabSheet;
    Label2: TLabel;
    Label5: TLabel;
    Label8: TLabel;
    RxDBCalcEdit1: TRxDBCalcEdit;
    DBDateEdit1: TDBDateEdit;
    Button2: TButton;
    Button4: TButton;
    DBEdit1: TDBEdit;
    TabSheet3: TTabSheet;
    Label6: TLabel;
    Label7: TLabel;
    Label9: TLabel;
    edNumparc: TEdit;
    Button5: TButton;
    Button6: TButton;
    edDiasParc: TEdit;
    edValorParc: TRxCalcEdit;
    dsParcela: TDataSource;
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fCadParcela: TfCadParcela;

implementation

uses uDm, uCadFinanceiro;

{$R *.dfm}

procedure TfCadParcela.Button1Click(Sender: TObject);
begin
  if dbValorParcela.Value = 0 then
   begin
      MessageDlg('Preencha o valor da parcela.',mtWarning,[mbOK],1);
      dbValorParcela.SetFocus;
      abort;
   end;
  if dbVencimento.Date = 0 then
   begin
      MessageDlg('Preencha o vencimento.',mtWarning,[mbOK],1);
      dbVencimento.SetFocus;
      abort;
   end;

  if fcadFinanceiro.tipo = 'RECEBER' then
  begin
    dm.qryFinanceiroNUMERO_DOC.AsString := dm.qryVendaNUM_NOTA.AsString;
    dm.qryFinanceiroPARCELA.AsString    := Edit1.Text;
    dm.qryFinanceiroVENDA_FK.AsInteger := dm.qryVendaCODIGO.AsInteger;
    dm.qryFinanceiroTIPO.AsString := 'C';
  end;

  if fcadFinanceiro.tipo = 'PAGAR' then
  begin
    dm.qryFinanceiroNUMERO_DOC.AsString := dm.qryCompraNUM_NOTA.AsString;
    dm.qryFinanceiroPARCELA.AsString    := Edit1.Text;
    dm.qryFinanceiroCOMPRA_FK.AsInteger  := dm.qryCompraCODIGO.AsInteger;
    dm.qryFinanceiroTIPO.AsString := 'D';
  end;

  dm.qryFinanceiroSITUACAO.AsString      := 'P';
  dm.qryFinanceiroVALOR_PARCELA.Value    := dbValorParcela.Value;
  dm.qryFinanceiro.Post;

  fCadFinanceiro.atualizaTotal;
  fCadFinanceiro.CarregaParcelas;

  Edit1.Text := IntToStr(StrToInt(Edit1.Text) + 1);
  dbValorParcela.Value := 0;
  dm.qryFinanceiro.Close;
  dm.qryFinanceiro.Open;
  dm.qryFinanceiro.Insert;
  dbValorParcela.SetFocus;
end;

procedure TfCadParcela.Button3Click(Sender: TObject);
begin
  dm.qryFinanceiro.Cancel;
  close;
end;

procedure TfCadParcela.Button5Click(Sender: TObject);
var
  dias,qtde_parc : integer;
  i : integer;
  valor_tot, valor_parc, soma, diferenca : Real;
  vencimento : TDate;
begin
  qtde_parc := StrToInt(edNumparc.Text);
  valor_tot  := edValorParc.Value;
  valor_parc := (valor_tot / qtde_parc);
  soma := 0;
  diferenca := 0;

  dias := StrToInt(edDiasParc.Text);
  vencimento := Date + dias;

  for i := 1 to StrToint(edNumparc.Text) do
  begin
      dm.exec.Close;
      dm.exec.SQL.Clear;
      dm.exec.SQL.Add('insert into financeiro (venda_fk,valor_parcela,venc_rec,situacao,numero_doc,tipo,parcela,compra_fk) values (:a,:b,:c,:d,:e,:f,:g,:h)');

      if fCadFinanceiro.tipo = 'RECEBER' then
      begin
        dm.exec.ParamByName('a').Value := dm.qryVendaCODIGO.AsInteger;
        dm.Exec.ParamByName('e').Value := dm.qryVendaNUM_NOTA.AsInteger;
        dm.Exec.ParamByName('f').Value := 'C';
      end;

      if fCadFinanceiro.tipo = 'PAGAR' then
      begin
        dm.exec.ParamByName('h').Value := dm.qryCompraCODIGO.AsInteger;
        dm.Exec.ParamByName('e').Value := dm.qryCompraNUM_NOTA.AsInteger;
        dm.Exec.ParamByName('f').Value := 'D';
      end;

      if i = StrToint(edNumparc.Text) then
      begin
        soma := StrToFloat(FormatFloat('#####0.00',valor_parc)) * StrToint(edNumparc.Text);
        if soma < valor_tot then
          diferenca := valor_tot - soma;
        dm.exec.ParamByName('b').Value := valor_parc + diferenca;
      end
      else
        dm.exec.ParamByName('b').Value := valor_parc;
      dm.Exec.ParamByName('c').Value := vencimento;
      dm.Exec.ParamByName('d').Value := 'P';
      dm.Exec.ParamByName('g').Value := i;
      dm.Exec.ExecSQL;
      vencimento := vencimento + dias;
     // valor_parc_tot := valor_parc_tot + valor_parc;
  end;
  fCadFinanceiro.atualizaTotal;
  fCadFinanceiro.CarregaParcelas;
  fCadFinanceiro.parcelaConsulta.Refresh;
  close;
end;

procedure TfCadParcela.Button2Click(Sender: TObject);
begin
  dm.qryFinanceiro.Post;
  close;
end;

procedure TfCadParcela.Button4Click(Sender: TObject);
begin
close;
end;

procedure TfCadParcela.Button6Click(Sender: TObject);
begin
  close;
end;

procedure TfCadParcela.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if (key = #13) then
  begin
    key := #0;
    Perform(WM_NEXTDLGCTL,0,0);
  end;

  if key = #27 then
   close;
end;

procedure TfCadParcela.FormShow(Sender: TObject);
begin
  if TabSheet1.TabVisible = true then
    dbValorParcela.SetFocus;
  if TabSheet2.TabVisible = true then
    RxDBCalcEdit1.SetFocus;
  if TabSheet3.TabVisible = true then
    edValorParc.SetFocus;
end;

end.

